<?php
namespace app\learn\controller;

use app\BaseController;
use think\facade\View;

class Download extends BaseController
{
    public function index()
    {
        return View::fetch('download/index');
    }

    public function download()
    {
        $file_path = root_path().'app\learn\view\download\物鱼奢品.zip';
        $range = request()->header('Range');
        if(is_null($range)){
            $filesize = filesize($file_path);
            header('Content-length:' . $filesize);
        }else{
            /*preg_match("/bytes=(\d+)-(\d+)/", "bytes=0-200", $matches);
            $start = $matches[1];
            $end = $matches[2];
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            $fp = fopen($file_path, 'r');
            // 定位到指定位置
            fseek($fp, $start);
            $buffer = fread($fp,$end-$start);
            echo $buffer;
            fclose($fp);*/
        }
    }

    public function test1()
    {
        $file_url = root_path().'app\learn\view\download\物鱼奢品.zip';  // 文件路径
        $fp = fopen($file_url, 'rb');
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . basename($file_url));
        header("Content-Length: " . filesize($file_url));
        while (!feof($fp)) {
            echo fread($fp, 8192);
        }
        fclose($fp);
    }
}
